package com.sumainfor.pc.market.controller.tenant;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumainfor.common.from.SearchHousingFrom;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.pc.market.service.HousingService;
import com.sumainfor.pc.market.service.tenant.TenantHousingService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 房源主页查询
 *
 * @Author Jack Chen
 * @Create 2019-01-29 14:26
 */
@RestController
@RequestMapping("/tenant/key/housingList")
public class HousingListController {

    @Autowired
    private TenantHousingService housingService;

    @Autowired
    HousingService HousingService;


    /**
     * 房源查询获取列表数据
     *
     * @param from 前端条件
     * @param page 分页
     * @return
     */
    @RequestMapping("/findHousing")
    public JsonResult housingSelectList(@ModelAttribute SearchHousingFrom from, Page page) {
        return housingService.housingSelectList(from, page);
    }

    /**
     * 根据编号查询房源的详细信息
     *
     * @param infoId
     * @return
     */
    @RequestMapping("/getHousingInfoById")
    public JsonResult getHousingInfoById(String infoId) {
        if (StringUtils.isBlank(infoId)) {
            return JsonResult.errorForEmpty();
        }
        return housingService.getHousingInfoById(infoId);
    }

    /**
     * 发起预约房源
     *
     * @param params
     * @return
     */
    @PostMapping("/makeHousing")
    public JsonResult makeHousing(@RequestBody Map<String, Object> params) {
        if (MapUtils.isEmpty(params)) {
            return JsonResult.errorForEmpty();
        }
        return housingService.makeHousing(params);
    }

    /**
     * 根据房源详表的编号查询 房源列表的数据
     *
     * @param params
     * @return
     */
    @PostMapping("/getHousingListByIds")
    public JsonResult getHousingListByIds(@RequestBody Map<String, Object> params) {
        List<String> ids = (List<String>) params.get("ids");
        if (CollectionUtils.isEmpty(ids)) {
            return JsonResult.errorForEmpty();
        }
        return housingService.getHousingListByIds(new Page(MapUtils.getLong(params, "current", 1L), MapUtils.getLong(params, "size", 10L)), ids);
    }

    /**
     * 获得房源预约列表数据
     *
     * @param tenId
     * @return
     */
    @PostMapping("/getMakeHousingList")
    public JsonResult getMakeHousingList(String tenId) {
        if (org.apache.commons.lang3.StringUtils.isBlank(tenId)) {
            return JsonResult.errorForEmpty();
        }
        return housingService.getMakeHousingList(tenId);
    }

    /**
     * 评价一个预约单
     *
     * @param params
     * @return
     */
    @PostMapping("/commentMakeOrder")
    public JsonResult commentMakeOrder(@RequestBody Map<String, Object> params) {
        return housingService.commentMakeOrder(params);
    }

    /**
     * 功能描述:
     * 根据租客编号获取租客当前合同列表
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/10 20:50
     */
    @ResponseBody
    @GetMapping("/getTenantPactList")
    public JsonResult getTenantPactList(@RequestParam Map<String, Object> params) {
        JsonResult result = new JsonResult();
        if (!FieldCheckUtil.isMapFieldNotNull(params, new String[]{"TenId"})) {
            result.putFailed("TenId不能为空！");
            return result;
        }
        result.put(HousingService.getTenantPactList(params));
        return result;
    }
}
